﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web_Demo
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtPass.Text = "";
                txtRePass.Text = "";
            }
        }

        protected void btRegister_Click(object sender, EventArgs e)
        {
            string username = HttpUtility.HtmlEncode(txtUserName.Text);
            string pass = txtPass.Text;
            string email = txtEmail.Text;
            DateTime date = DateTime.Now;

            DMSdataDataContext dms = new DMSdataDataContext();
            if (dms.DMS_Users.Where(p => p.Name_User == username).Count() == 1)
            {
                Response.Write("<script>alert('username is available')</script>");

            }
            else
            {
                DMS_User RegiUser = new DMS_User()
                {
                    Name_User = HttpUtility.HtmlDecode(username),
                    Pass_User = pass,
                    Email = email,
                    CreatedOndate = date,
                    ID_Role_User = 2,
                };
                dms.DMS_Users.InsertOnSubmit(RegiUser);
                dms.SubmitChanges();
                if (RegiUser != null)
                {
                    string filePath = Server.MapPath(@"/Document/" + txtUserName.Text);
                    int iduser = (int)(dms.DMS_Users.Single(p => p.Name_User == txtUserName.Text).ID_User);
                    DMS_Folder dmsfolder1 = new DMS_Folder()
                    {
                        Name_Folder = txtUserName.Text,
                        CreatedOnDate = DateTime.Now,
                        Folder_Path = filePath,

                    };


                    DMS_User_Folder dmsuserfolder1 = new DMS_User_Folder()
                    {
                        ID_User = iduser,
                        ID_Role_Folder = 1,



                    };
                    dmsfolder1.DMS_User_Folders.Add(dmsuserfolder1);
                    dms.DMS_Folders.InsertOnSubmit(dmsfolder1);
                    dms.SubmitChanges();
                    int idfolder = dms.DMS_Folders.Single(p => p.Name_Folder == txtUserName.Text).ID_Folder;
                    DMS_Folder dmsfolder = new DMS_Folder()
                    {
                        Name_Folder = "Share",
                        CreatedOnDate = DateTime.Now,  
                        ID_ParentFolder = idfolder,
                        Folder_Path = filePath + "\\Share",
                    };

                    

                    DMS_User_Folder dmsuserfolder = new DMS_User_Folder()
                    {                        
                        ID_User = iduser,
                        ID_Role_Folder = 1,
                        


                    };
                    dmsfolder.DMS_User_Folders.Add(dmsuserfolder);
                    dms.DMS_Folders.InsertOnSubmit(dmsfolder);
                    dms.SubmitChanges();

                    
                    Directory.CreateDirectory(filePath);
                    Directory.CreateDirectory(filePath+"\\Share");
                    
                    Response.Write("<script>alert('Register successfull')</script>");
                    Response.Redirect("Login.aspx");
                }
                else Response.Write("<script>alert('Register fail')</script>");
            }
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {

        }
    }
}